﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.ApproveCommand;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.ApproveCommandHandler
{
    /// <summary>
    /// 通过请假申请id查询相关记录表条数 (申请id ==> 记录条数)
    /// </summary>
    public class GetLeaveRecordCountCommandHandler : IRequestHandler<GetLeaveRecordCountCommand, APIResult<string>>
    {
        private readonly IBaseRepository<LeaveRecordModel> recorRepos;

        public GetLeaveRecordCountCommandHandler(IBaseRepository<LeaveRecordModel> recorRepos)
        {
            this.recorRepos = recorRepos;
        }

        /// <summary>
        /// 通过请假申请id查询相关记录表条数 (申请id ==> 记录条数)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<string>> Handle(GetLeaveRecordCountCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var list = await recorRepos.GetAll().Result.Where(x=>x.LeaveId == request.LeaveId).ToListAsync();
            result.data = list.Count().ToString();
            return await Task.FromResult(result);
        }
    }
}
